package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品三级分类
 *
 * @author 雨落无阁
 * @email 2801897707@gmail.com
 * @date 2022-09-19 16:58:00
 */
@Api(tags = "商品分类")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 树形结构
     */
    @ApiOperation("树状结构")
    @GetMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R tree(){

        List<CategoryEntity> tree = categoryService.listWithTree();


        return R.ok().put("data",tree);
    }
    /**
     * 更新排序
     *
     */
    @ApiOperation("更新排序")
    @PostMapping("/sort")
    public R sort(@RequestBody List<CategoryEntity> categories){
        categoryService.updateBatchById(categories);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @ApiOperation("添加分类")
    @PostMapping
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改分类")
    @PutMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除分类")
    @DeleteMapping
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //1。

		categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
